package com.luzhikun.ajax.servlet;

import com.luzhikun.ajax.service.IBmiService;
import com.luzhikun.ajax.service.IBmiServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/*
 * @Author liu-miss
 * @Description 通过ajax发送异步请求
 * @Date 14:20 2021/5/9
 **/
@WebServlet(name = "BmiServlet", value = "/bmiAjax")
public class BmiAjaxServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String name = request.getParameter("name");
        String weight = request.getParameter("w");
        String height = request.getParameter("h");
        // 格式化
        double userweight=Double.parseDouble(weight);
        double userheight=Double.parseDouble(height);
        // 调用service层对象处理数据
        IBmiService is=new IBmiServiceImp();
        double bmiCount = is.getBmiCount(userweight, userheight);
        String msg="";
        if (bmiCount<=18.5){
            msg="您比较瘦";
        }else if (bmiCount>=18.5 && bmiCount<=23.9){
            msg="您的身体正常";
        }else if (bmiCount>24 && bmiCount<=27 ){
            msg="您的身体比较胖";
        }else {
            msg="您的身体肥胖";
        }

        PrintWriter printWriter=response.getWriter();
        printWriter.println(msg);
        printWriter.flush();
        printWriter.close();
    }
}
